package com.vang.chue.capm.dao.impl;

import com.vang.chue.capm.dao.PersonDao;
import com.vang.chue.capm.entity.Person;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: chuevang
 * Date: 11/5/11
 * Time: 8:32 PM
 * To change this template use File | Settings | File Templates.
 */

@Repository
public class PersonHibernateDao implements PersonDao {

    @Autowired
    private SessionFactory sessionFactory;

    public Person findById(Long id)
    {
        List<Person> result = this.sessionFactory.getCurrentSession().createSQLQuery("* from person where id = :id").setLong("id", id).list();
        Person p = null;
        if (null != result && result.size() > 0)
        {
            p = result.get(0);
        }

        return p;
    }

    public Person persistOrMerge(Person person)
    {
        person.setSex("M");
        this.sessionFactory.getCurrentSession().saveOrUpdate(person);
        this.sessionFactory.getCurrentSession().flush();
        return person;
    }


    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
